<template>
	<view class="charts-box">
		<qiun-data-charts type="pie" :opts="opts" :chartData="chartData" />
	</view>
</template>

<script>
	export default {
		data() {
			return {
				chartData: {},
				//您可以通过修改 config-ucharts.js 文件中下标为 ['pie'] 的节点来配置全局默认参数，如都是默认参数，此处可以不传 opts 。实际应用过程中 opts 只需传入与全局默认参数中不一致的【某一个属性】即可实现同类型的图表显示不同的样式，达到页面简洁的需求。
				opts: {
					color: ["#1890FF", "#91CB74", "#FAC858", "#EE6666", "#73C0DE", "#3CA272", "#FC8452", "#9A60B4",
						"#ea7ccc"
					],
					padding: [5, 5, 5, 5],
					enableScroll: false,
					extra: {
						pie: {
							activeOpacity: 0.5,
							activeRadius: 10,
							offsetAngle: 0,
							labelWidth: 15,
							border: false,
							borderWidth: 3,
							borderColor: "#FFFFFF"
						}
					},
					legend: {
					    show: true,
					    position: 'left', // You can set 'top', 'bottom', 'left', 'right', or 'center'
					    offsetY: 50, 
						lineHeight: 25,
					  },
				}
			};
		},

		created() {
			this.getServerData();
		},
		methods: {

			getServerData() {
				const token = uni.getStorageSync('token')
				uni.request({
					url: "http://101.126.42.213:9090/dataStatistics/sexAge",
					method: "GET",
					header: {
						'Token': `${token}` // 在请求头中添加token，注意Bearer后面有一个空格
					},
					success: (res) => {
						console.log(res, '性别数据')
						const apiData = res.data;
						setTimeout(() => {
						    // 模拟服务器返回数据，如果数据格式和标准格式不同，需自行按下面的格式拼接
						    let res = {
						      series: [
						        {
						          data: [
						            { "name": "20岁以下", "value": apiData.man20 + apiData.women20 },
						            { "name": "20-30岁", "value": apiData.man20to30 + apiData.women20to30 },
						            { "name": "30-40岁", "value": apiData.man30to40 + apiData.women30to40 },
						            { "name": "40-50岁", "value": apiData.man40to50 + apiData.women40to50 },
						            { "name": "其他", "value": apiData.manelse + apiData.womenelse },
						          ]
						        }
						      ]
						    };
						    this.chartData = JSON.parse(JSON.stringify(res));
						  }, 500);

					},
					fail: (error) => {
						console.log(error, '请求失败');
					},
				});
			},
		},
	};
</script>

<style scoped>
	.charts-box {
		width: 100%;
		height: 350rpx;
	}
</style>